package com.yogee.topseller.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.model.bean.InterestLabelBean;
import com.yogee.topseller.view.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/21.
 */

public class InterestLabelAdapter extends RecyclerView.Adapter<InterestLabelAdapter.LabelViewHolder> {
    //上下文
    Context mContext;
    //标签数据
    List<InterestLabelBean.ListBean> mLabelDatas;
    //已选中的标签
    public ArrayList<TextView> mSelectView = new ArrayList<>();

    private ArrayList<InterestLabelBean.ListBean.LabelListBean> mSelectBean = new ArrayList<>();

    public InterestLabelAdapter(Context context) {
        this.mContext = context;
    }

    public void addData(List<InterestLabelBean.ListBean> mLabelDatas) {
        this.mLabelDatas = new ArrayList<>();
        this.mLabelDatas = mLabelDatas;
        notifyDataSetChanged();
    }

    public ArrayList<InterestLabelBean.ListBean.LabelListBean> getmSelectBean() {
        return mSelectBean;
    }

    @Override
    public void onBindViewHolder(final LabelViewHolder holder, final int position) {

        holder.mLabelType.setText(mLabelDatas.get(position).getType());
        holder.mLabelData.setData(mLabelDatas.get(position).getLabelList());
        holder.mLabelData.setOnTagClickListener(new FlowLayout.OnTagClickListener() {
            @Override
            public void TagClick(InterestLabelBean.ListBean.LabelListBean bean, TextView textView) {
                mSelectBean.add(bean);
                textView.setTextColor(mContext.getResources().getColor(R.color.select_color));
                textView.setBackgroundResource(R.drawable.corens_100_style_select);
                Drawable dra = ContextCompat.getDrawable(mContext,R.drawable.select);
                dra.setBounds(8,0,0,0);
                textView.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(mContext,R.drawable.select),null,null,null);

                if (!mSelectView.contains(textView)) {
                    mSelectView.add(textView);

                } else {
                    if (mSelectBean.contains(bean)) {
                        mSelectBean.remove(bean);
                    }
                    textView.setBackgroundResource(R.drawable.corens_100_style_unselect);
                    textView.setTextColor(Color.parseColor("#4a4a4a"));
                    textView.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
                    mSelectView.remove(textView);
                }
            }
        });
    }

    @Override
    public LabelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        item_interest_label
        LabelViewHolder holder = new LabelViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.item_interest_label_new, parent,
                false));
        return holder;
    }

    @Override
    public int getItemCount() {
        return mLabelDatas == null ? 0 : mLabelDatas.size();
    }

    class LabelViewHolder extends RecyclerView.ViewHolder {

        TextView mLabelType,tv_more;
        FlowLayout mLabelData;


        public LabelViewHolder(View view) {
            super(view);
            mLabelType = (TextView) view.findViewById(R.id.tv_label_title);
            mLabelData = (FlowLayout) view.findViewById(R.id.fl_label_data);
            tv_more = (TextView) view.findViewById(R.id.tv_more);
        }
    }
}
